﻿var status = -1;
var selectedType = 0;
var selectedMeso = 0;
var moneyMeso = 30000000;

function start() {
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == -1) {
        cm.dispose();
    } else {
        if (mode == 0 && status == 0) {
            cm.dispose();
            return;
        }
        if (mode == 1) {
            status++;
        } else {
            if (status == 2) {
                cm.sendNext("如果您需要兑换国庆纪念币的话请记得来找我");
                cm.dispose();
            }
            status--;
        }
        if (status == 0) {
            cm.sendSimple("亲爱的#r#h ##k您好，我是国庆纪念币销售员，如果您需要兑换国庆纪念币的话请找我，请选择您需要操作的项目:\r\n#r提示：金币最大上限是99E，无论是店铺取回的，还是其他方式，注意看自己的金币数量，否则超过了最大上限会被吃掉。\r\n#b#L1#我要购买国庆纪念币#l\r\n#L2#我要将国庆纪念币兑换成金币#l");
        } else if (status == 1) {
            selectedType = selection;
            if (selectedType == 0) {
                if (cm.getBossLog("每天领取国庆纪念币") == 0) {
                    cm.gainItem(4000463, 1);
                    cm.setBossLog("每天领取国庆纪念币");
                    cm.sendOk("#fUI/UIWindow2.img/QuestIcon/4/0#\r\n#v4000463#国庆纪念币x1");
                    cm.dispose();
                } else {
                    cm.sendOk("你今天已经领取过国庆纪念币。");
                    cm.dispose();
                }
            } else if (selectedType == 1) {
                cm.sendGetNumber("你要购买多少个国庆纪念币呢:\r\n#r当前物价比：#v4000463# x #r1 #k= #v2140002##r3000W\r\n你当前拥有#v2140002# x #r" + cm.getMeso(), 1, 1, 333);
            } else if (selectedType == 2) {
                cm.sendGetNumber("你要出售多少个国庆纪念币呢:\r\n#r当前物价比：#v4000463# x #r1 #k= #v2140002##r3000W\r\n你当前拥有#v4000463# x #r" + cm.itemQuantity(4000463), cm.itemQuantity(4000463), 1, 333);
            }
        } else if (status == 2) {
            selectedMeso = selection;
            if (selectedType == 1) {
                cm.sendYesNo("您是否要购买#r " + selectedMeso + " #k个#v4000463#。");
            } else if (selectedType == 2) {
                cm.sendYesNo("您是否要出售#r " + selectedMeso + " #k个#v4000463#。");
            }
        } else if (status == 3) {
            if (selectedType == 1) {
                if (cm.getMeso() < selectedMeso * moneyMeso) {
                    cm.sendNext("您的金币不够。");
                } else {
		    cm.gainItem(4000463, selectedMeso)
                    cm.gainMeso(-selectedMeso * moneyMeso);
                    cm.sendOk("购买成功");
                }
                cm.dispose();
            } else if (selectedType == 2) {
                if (cm.itemQuantity(4000463) < selectedMeso) {
                    cm.sendNext("你确定有那么多国庆纪念币么");
                } else if ((9999999999 - cm.getMeso()) / moneyMeso < selectedMeso) {
		    var i1 = (9999999999 - cm.getMeso()) / moneyMeso;
		    var i2 = ((9999999999 - cm.getMeso()) % moneyMeso) / moneyMeso;
                    cm.sendNext("超出最大金额值，您只能出售#r" + (i1 - i2) + "#k个#v4000463#");
                } else {
                    cm.gainMeso(selectedMeso * moneyMeso);
		    cm.gainItem(4000463, -selectedMeso)
                    cm.sendOk("兑换成功");
                }
                cm.dispose();
            }
        } else {
            cm.dispose();
        }
    }
}